package com.lightdz.tshirtdz.dal.impl;

import com.lightdz.tshirtdz.model.Enum.BaseStatusEnum;
import com.lightdz.tshirtdz.model.PageInfo;
import com.lightdz.tshirtdz.model.api.ApiShoppingCar;
import com.lightdz.tshirtdz.model.entity.ShoppingCarEntity;

import com.lightdz.tshirtdz.utils.MybatisDBHelper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @ Description：${description}
 * @ Author     ：zbl
 * @ Date       ：Created in 16:54 09/08/2018
 * @ Modified By：
 */

@Repository
public class ShoppingCarDal {

    /**
     *
     * @param wxId
     * @param pageInfo
     * @return
     */
    public List<ShoppingCarEntity> getShoppingCarList(String wxId, PageInfo pageInfo){
        ShoppingCarEntity entity=new ShoppingCarEntity();
        entity.setWxid(wxId);

        //分页查询
        int offset=0;
        int limit=0;
        if(pageInfo.getPageNo()>1 && pageInfo.getPageSize()>0) {
            offset = (pageInfo.getPageNo()-1) * pageInfo.getPageSize();// offset起始行
        }
        limit = pageInfo.getPageSize(); // limit是当前页显示多少条数据
        RowBounds rowBounds = new RowBounds(offset, limit);

        List<ShoppingCarEntity> list  =(List<ShoppingCarEntity>) MybatisDBHelper.SelectPageList("getShoppingCarList",entity,rowBounds);
        return  list;
    }
    public ShoppingCarEntity getOneShoppingCar(String wxId, long pid){
        ShoppingCarEntity tempEntity=new ShoppingCarEntity();
        tempEntity.setWxid(wxId);
        tempEntity.setPid(pid);
        ShoppingCarEntity entity  = (ShoppingCarEntity)MybatisDBHelper.SelectOne("getShoppingCarList",tempEntity);
        return  entity;
    }
    public boolean addShoppingCar(String wxId, ApiShoppingCar shoppingCar){
        ShoppingCarEntity entity=new ShoppingCarEntity();
        entity.setWxid(wxId);
        entity.setCreateTime(new Timestamp(new Date().getTime()));
        entity.setpColor(shoppingCar.getpStyle().getColorList().get(0));
        entity.setpCount(shoppingCar.getpStyle().getpCount());
        entity.setPid((int)shoppingCar.getpStyle().getPid());
        entity.setpSize(shoppingCar.getpStyle().getSizeList().get(0));
        entity.setStatus(BaseStatusEnum.Normal.getValue());
        entity.setUpdateTime(new Timestamp(new Date().getTime()));

        int result = MybatisDBHelper.Add("addShoppingCar",entity);
        return  result>0;
    }

    public boolean deleteShoppingCar(String wxId,long pid){
        ShoppingCarEntity entity=new ShoppingCarEntity();
        entity.setPid((int)pid);
        entity.setWxid(wxId);
        int result = MybatisDBHelper.Delete("deleteShoppingCar",entity);
        return  result>0;
    }

    public boolean updateShoppingCar(String wxId,long pid,int pCount){
        ShoppingCarEntity entity=new ShoppingCarEntity();
        entity.setPid((int)pid);
        entity.setpCount(pCount);
        entity.setWxid(wxId);
        int result = MybatisDBHelper.Update("updateShoppingCar",entity);
        return  result>0;
    }
}
